package com.bootdo.contract.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.contract.domain.YutableDO;
import com.bootdo.contract.service.YutableService;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.contract.domain.YumemberDO;
import com.bootdo.contract.service.YumemberService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * @author yang
 * @email 2795210596@qq.com
 * @date 2019-09-03 13:27:53
 */

@Controller
@RequestMapping ( "/contract/yumember" )
public class YumemberController {
    @Autowired
    private YumemberService yumemberService;
    @Autowired
    private UserService userService;
    @Autowired
    private YutableService yutableService;

    @GetMapping ()
    @RequiresPermissions ( "contract:yumember:yumember" )
    String Yumember() {
        return "contract/yumember/yumember";
    }

    @ResponseBody
    @GetMapping ( "/list" )
    @RequiresPermissions ( "contract:yumember:yumember" )
    public PageUtils list(@RequestParam Map <String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List <YumemberDO> yumemberList = yumemberService.list(query);
        int total = yumemberService.count(query);
        PageUtils pageUtils = new PageUtils(yumemberList, total);
        return pageUtils;
    }

    @GetMapping ( "/add" )
    @RequiresPermissions ( "contract:yumember:add" )
    String add(@RequestParam ( "yuid" ) Integer yuid, Model model) {
        List <UserDO> userList = userService.list();
        model.addAttribute("userList", userList);
        model.addAttribute("id", yuid);
        return "contract/yumember/add";
    }

    @GetMapping ( "/edit/{id}" )
    @RequiresPermissions ( "contract:yumember:edit" )
    String edit(@PathVariable ( "id" ) Integer id, Model model) {
        YumemberDO yumember = yumemberService.get(id);
        model.addAttribute("yumember", yumember);
        return "contract/yumember/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping ( "/save" )
    @RequiresPermissions ( "contract:yumember:add" )
    public R save(YumemberDO yumember, @RequestParam ( "manager" ) String manager) {
        String[] split = manager.split(",");
        yumember.setRenyuanid(Integer.parseInt(split[0]));
        yumember.setRenyuanname(split[1]);
        YutableDO yutableDO = yutableService.get(yumember.getYuid());
        if (yutableDO != null) {
            yumember.setYuname(yutableDO.getName());
            System.out.println(yutableDO.toString());
        }
        System.out.println(yumember.toString());
        Map <String, Object> map = new HashMap();
        map.put("yuid", yumember.getYuid());
        map.put("renyuanid", yumember.getRenyuanid());
        List <YumemberDO> list = yumemberService.list(map);
        if (list.size() > 0) {
            return R.error("不能重复添加域成员");
        } else {
            if (yumemberService.save(yumember) > 0) {
                return R.ok();
            } else {
                return R.error();
            }
        }


    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping ( "/update" )
    @RequiresPermissions ( "contract:yumember:edit" )
    public R update(YumemberDO yumember) {
        yumemberService.update(yumember);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping ( "/remove" )
    @ResponseBody
    @RequiresPermissions ( "contract:yumember:remove" )
    public R remove(Integer id) {
        if (yumemberService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping ( "/batchRemove" )
    @ResponseBody
    @RequiresPermissions ( "contract:yumember:batchRemove" )
    public R remove(@RequestParam ( "ids[]" ) Integer[] ids) {
        yumemberService.batchRemove(ids);
        return R.ok();
    }

}
